package imageEditor.mapEditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import imageEditor.mapEditor.model.Map;
import imageEditor.utility.ImagePainer;
import imageEditor.utility.Shape;
import imageEditor.utility.Shape.shapeType;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MapPainer extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	private ImagePainer painer;
	private JButton addGate;
	private JButton addPlatform;
	private JButton addStair;
	private ArrayList<Shape> shapes;
	private JPanel south;
	private JButton getCode;
	private Map map;
	
	public MapPainer(){
		super();
		painer=new ImagePainer(this);
		shapes=new ArrayList<Shape>();
		
		this.setLayout(new BorderLayout());
		this.add(painer,"Center");
		
	}
	
	private JPanel getMenu(){
		addGate=new JButton("add gate");
		addGate.addActionListener(this);
		addPlatform=new JButton("add platform");
		addPlatform.addActionListener(this);
		addStair=new JButton("add stair");
		addStair.addActionListener(this);
		
		//getCode=new JButton("get code");
		//getCode.addActionListener(this);
		
		JPanel res=new JPanel();
		res.setLayout(new FlowLayout());
		res.add(addGate);
		res.add(addPlatform);
		res.add(addStair);
		//res.add(getCode);
		
		return res;
	}

	public void setMap(Map map) {
		if(south==null){
			south=getMenu();
			this.add(south,"South");
		}
		
		this.map=map;
		
		shapes=map.getShapes();
		
		addPlatform.setEnabled(true);
		addGate.setEnabled(true);
		addStair.setEnabled(true);
		

		painer.setImage(map.getImage());
		
		repaint();
		validate();
	}

	public void actionPerformed(ActionEvent e) {
		Object source=e.getSource();
		
		if(source==addPlatform){
			Shape s=new Shape(0,0,10,10,shapeType.rectangle, Color.BLUE, null);
			map.addPlatform(s);
			painer.startDrawPlatform(s);
			
			addPlatform.setEnabled(false);
			addGate.setEnabled(true);
			addStair.setEnabled(true);
		}
		else if(source==addGate){
			String s=(String)JOptionPane.showInputDialog(
					this,
					"Set the map id of the destiantion",
					"Destinatio id",
					JOptionPane.PLAIN_MESSAGE,
					null,
					null,
					"");
			
			s+="@";
			
			s+=(String)JOptionPane.showInputDialog(
					this,
					"Set the destination point",
					"Destinatio point",
					JOptionPane.PLAIN_MESSAGE,
					null,
					null,
					"");
			
			Shape sha=new Shape(0,0,10,10,shapeType.rectangle, Color.RED, s);
			map.addGate(sha);
			painer.startDrawGate(sha);
			
			addPlatform.setEnabled(true);
			addGate.setEnabled(true);
			addStair.setEnabled(true);
			
		}
		else if(source==addStair){
			Shape s=new Shape(0,0,10,10,shapeType.rectangle, Color.GREEN, null);
			map.addStair(s);
			painer.startDrawStair(s);
			
			addPlatform.setEnabled(true);
			addGate.setEnabled(true);
			addStair.setEnabled(false);
		}
		else if(source==getCode){
			
			
			JOptionPane.showMessageDialog(
					this,
					map.getCode(),
					"Map code",
					JOptionPane.PLAIN_MESSAGE);
		}
	}

	public ArrayList<Shape> getShapes() {
		return shapes;
	}

	public void remove(Shape tmp) {
		map.remove(tmp);
	}

}
